package exser;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.HashMap;

/**
 * @author: knowno
 * @create: 2025-09-17 11:01
 * @Version 1.0
 **/
public class TestSerializable2 {
    public static void main(String[] args) {

        //创建一个User对象
        User user = new User("1", "knowno", "123");
        //再创建一个User对象
        User user2 = new User("2", "knowno2", "123");

        //使用HashMap来存储两个对象
        HashMap<String, User> map = new HashMap<>();
        map.put(user.getId(), user);
        map.put(user2.getId(), user2);

        try {
            //1 ObjectOutputStream对象创建
            ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("user.txt"));
            //2 调用writeObject方法实现序列化操作
            oos.writeObject(map);
            //3 释放资源
            oos.close();

            System.out.println("序列化成功");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
